package by.dezhits.liltweets.auth;

import android.os.AsyncTask;

public class TwitterRecieveAccessTokenTask extends
		AsyncTask<TwitterCallback<String[]>, Integer, Boolean> {
	private TwitterCallback<String[]> callback;

	private String verifier;

	public TwitterRecieveAccessTokenTask(String v) {
		super();
		this.verifier = v;
	}

	@Override
	protected Boolean doInBackground(TwitterCallback<String[]>... i) {
		this.callback = i[0];
		try {
			TwitterAuthHelper.getProvider().retrieveAccessToken(
					TwitterAuthHelper.getConsumer(), verifier);
		} catch (Exception e) {
			return false;
		}
		return true;

	}

	@Override
	protected void onPostExecute(Boolean res) {
		if (res) {
			String userId = TwitterAuthHelper.getToken().substring(0,
					TwitterAuthHelper.getToken().indexOf("-"));
			String token = TwitterAuthHelper.getToken();
			String tokenSecret = TwitterAuthHelper.getTokenSecret();
			this.callback
					.onSuccess(new String[] { userId, token, tokenSecret });
		}
		else{
			this.callback.onError();
		}
	}
}